package zhoufu.sz.sanxiao_check.ui;

import android.content.DialogInterface;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import java.util.List;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityStreetDetailsBinding;
import zhoufu.sz.sanxiao_check.inject.HasComponent;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.IDModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.models.StreetListModel;
import zhoufu.sz.sanxiao_check.models.User;
import zhoufu.sz.sanxiao_check.presents.StreetInfoPresenter;
import zhoufu.sz.sanxiao_check.ui.basic.BasicMainActivity;
import zhoufu.sz.sanxiao_check.ui.basic.PlaceChangeActivity;
import zhoufu.sz.sanxiao_check.ui.check.CheckMainActivity;
import zhoufu.sz.sanxiao_check.ui.contract.StreetDetailsContract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/22
 */
public class StreetDetailsActivity extends BaseActivity
        implements StreetDetailsContract.View, HasComponent<LoginComponet>, View.OnClickListener
{

    private ActivityStreetDetailsBinding _binding;

    @Inject
    StreetInfoPresenter _presenter;

    private StreetListModel mStreetListModel;

    LoginComponet componet;

    private MenuItem item;

    @Override protected void initInject()
    {
        mStreetListModel = getIntent().getParcelableExtra("StreetListModel");
        componet = DaggerLoginComponet.builder().activityModule(getActivityModule())
                .appComponent(getAppComponent()).build();
        componet.inject(this);
        _presenter.bindView(this);
        _presenter.onStart();
    }

    @Override protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if ( savedInstanceState != null )
        {
            mStreetListModel = savedInstanceState.getParcelable("StreetListModel");
        }
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        super.initAfterOnCreate(binding);
        _binding = (ActivityStreetDetailsBinding) binding;
        _binding.setCname(User.readUser().getCName());
        _binding.setOnClickListener(this);
        _binding.multiplestatusview.setOnRetryClickListener(this);
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_street_details;
    }

    @Override public String Building_No()
    {
        return mStreetListModel.getBuilding_No();
    }

    @Override public String RID()
    {
        return mStreetListModel.getRID();
    }

    @Override public String SID()
    {
        return mStreetListModel.getSID();
    }

    @Override public void setData(StreetInfoModel model)
    {
        _binding.multiplestatusview.showContent();
        _binding.setModel(model == null ? new StreetInfoModel() : model);
        setTitle(model);
        _binding.setUrl(
                TextUtils.isEmpty(model.getSID()) ? model.getStorePhoto() : model.getStorefront());
    }

    @Override public void event(IDModel model)//场所变更通知
    {
        _presenter.setSID(model.getSID());
        _presenter.onStart();
    }

    @Override public void showDialog(final String path)
    {
        new AlertDialog.Builder(this).setTitle("门店照片").setMessage("门店更新失败,是否确定重新上传？")
                .setNegativeButton(
                        R.string.dialog_ok, new DialogInterface.OnClickListener()
                        {
                            @Override public void onClick(DialogInterface dialog, int which)
                            {
                                _presenter.uploadPhoto(path);
                                dialog.dismiss();
                            }
                        }).create().show();
    }


    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        hideProgreesDialog();
        _binding.multiplestatusview.showError();
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }

    @Override protected void onStart()
    {
        super.onStart();
    }

    @Override protected void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }

    @Override public LoginComponet getComponent()
    {
        return componet;
    }

    @Override public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.mIvPhoto://查看大图
                if ( _binding.getModel() == null )
                {
                    return;
                }
                _navigator.startShowPic(this, _binding.getUrl());
                break;
            case R.id.mIvChangeIcon://修改门店图片
                FunctionConfig config = new FunctionConfig.Builder()
                        .setEnableCamera(true)
                        .setMutiSelectMaxSize(1)
                        .build();
                GalleryFinal.openGalleryMuti(1, config, mResultCallback);
                break;
            case R.id.mBtnPeopleNum:
                _presenter.updatePeopleNum(
                        _binding.mRgState.getCheckedRadioButtonId() == R.id.mRbPeopleYes ? 1 : 0,
                        _binding.mEtPeopleNum.getText().toString());
                break;
            case R.id.tvStreetDetailTab1://消防检查
                _navigator.startBasicMainAct(this, _binding.getModel(), CheckMainActivity.class);
                break;
            case R.id.tvStreetDetailsTab2://信息录入
                _navigator.startBasicMainAct(this, _binding.getModel(), BasicMainActivity.class);
                break;
            case R.id.tvStreetDetailTab3://场所变更
                _navigator.startBasicMainAct(this, _binding.getModel(), PlaceChangeActivity.class);
                break;
            case R.id.tvStreetDetailTab4://空铺住人
                new AlertDialog.Builder(this).setTitle("封存店铺").setMessage("将场所列为空铺")
                        .setNegativeButton(
                                R.string.dialog_ok, new DialogInterface.OnClickListener()
                                {
                                    @Override public void onClick(DialogInterface dialog, int which)
                                    {
                                        _presenter.StreeClose();
                                        dialog.dismiss();
                                    }
                                }).create().show();
                break;
            case R.id.error_view:
                _presenter.onStart();
                break;
        }
    }

    private void setTitle(StreetInfoModel model)
    {
        if ( TextUtils.isEmpty(model.getSID()) )
        {
            setTitle("空铺");
            //item.setVisible(false);
        } else
        {
            setTitle(model.getName());
            //item.setVisible(true);
        }
    }

    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        //getMenuInflater().inflate(R.menu.menu_train, menu);
        //
        //item = menu.findItem(R.id.menu_train);

        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        //if ( item.getItemId() == R.id.menu_train )
        //{
        //    _navigator.startBasicMainAct(this, _binding.getModel(), TrainActivity.class);
        //}
        return super.onOptionsItemSelected(item);
    }

    private GalleryFinal.OnHanlderResultCallback mResultCallback
            = new GalleryFinal.OnHanlderResultCallback()
    {
        @Override public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList)
        {
            if ( resultList != null && resultList.size() > 0 )
            {
                _presenter.uploadPhoto(resultList.get(0).getPhotoPath());
                _binding.setUrl(resultList.get(0).getPhotoPath());
                if ( TextUtils.isEmpty(_binding.getModel().getSID()) )
                {
                    _binding.getModel().setStorePhoto(_binding.getUrl());
                } else
                {
                    _binding.getModel().setStorefront(_binding.getUrl());
                }
            }
        }

        @Override public void onHanlderFailure(int requestCode, String errorMsg)
        {
            UIUtils.showSnackbar(_binding.getRoot(), errorMsg);
        }
    };

    @Override protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putParcelable("StreetListModel", mStreetListModel);
    }
}
